import request from '@/utils/request'

// ==================== 学生个人资料管理相关API ====================

// 获取当前登录学生个人资料
export function getCurrentStudentProfile() {
  return request({
    url: '/campus/student/profiles/current',
    method: 'get'
  })
}

// 获取学生个人资料 (学生专用接口)
export function getStudentProfile(studentId) {
  console.log('Calling getStudentProfile with studentId:', studentId)
  // 更严格的用户ID验证，与前端保持一致
  if (!studentId || 
      studentId === 'undefined' || 
      studentId === 'null' || 
      studentId === '' || 
      (typeof studentId === 'string' && studentId.trim() === '') ||
      isNaN(studentId)) {
    console.error('Invalid studentId provided to getStudentProfile:', studentId)
    // 返回一个拒绝的Promise，避免发送无效请求
    return Promise.reject(new Error('Invalid studentId: ' + studentId))
  }
  return request({
    url: '/campus/student/profiles/' + studentId,
    method: 'get'
  })
}

// 查询学生详细
export function getStudent(studentId) {
  // 更严格的用户ID验证，与前端保持一致
  if (!studentId || 
      studentId === 'undefined' || 
      studentId === 'null' || 
      studentId === '' || 
      (typeof studentId === 'string' && studentId.trim() === '') ||
      isNaN(studentId)) {
    console.error('Invalid studentId provided to getStudent:', studentId)
    // 返回一个拒绝的Promise，避免发送无效请求
    return Promise.reject(new Error('Invalid studentId: ' + studentId))
  }
  
  const url = '/campus/student/' + studentId
  console.log('API Call - getStudent URL:', url)
  console.log('API Call - studentId parameter:', studentId)
  
  return request({
    url: url,
    method: 'get'
  })
}

// 修改学生
export function updateStudent(data) {
  return request({
    url: '/campus/student',
    method: 'put',
    data: data
  })
}

// 更新学生个人资料
export function updateStudentProfile(data) {
  return request({
    url: '/campus/student/profiles',
    method: 'put',
    data: data
  })
}

// 上传学生头像
export function uploadStudentAvatar(data) {
  return request({
    url: '/campus/student/avatar',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data: data
  })
}

// 获取学生门户首页数据
export function getStudentPortalData(studentId) {
  return request({
    url: '/campus/student/portalData/' + studentId,
    method: 'get'
  })
}

// 验证学生邮箱唯一性
export function checkStudentEmail(email, studentId) {
  return request({
    url: '/campus/student/checkEmail',
    method: 'get',
    params: {
      email,
      studentId
    }
  })
}

// 验证学生手机号唯一性
export function checkStudentPhone(phone, studentId) {
  return request({
    url: '/campus/student/checkPhone',
    method: 'get',
    params: {
      phone,
      studentId
    }
  })
}

// 验证学号唯一性
export function checkStudentNumber(studentNumber, studentId) {
  return request({
    url: '/campus/student/checkStudentNumber',
    method: 'get',
    params: {
      studentNumber,
      studentId
    }
  })
}

// 添加默认导出以确保模块正确导入
export default {
  getCurrentStudentProfile,
  getStudentProfile,
  getStudent,
  updateStudent,
  updateStudentProfile,
  uploadStudentAvatar,
  getStudentPortalData,
  checkStudentEmail,
  checkStudentPhone,
  checkStudentNumber
}